package com.simtop.car.controller;

import com.simtop.car.common.ServerResponse;
import com.simtop.car.pojo.*;
import com.simtop.car.service.AdminService;
import com.simtop.car.utils.FileUploadAndDownload;
import com.simtop.car.utils.JwtUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
@CrossOrigin
@RequestMapping(value = "/admin")
public class AdminController {

    @Autowired
    public AdminService adminService;


    @Autowired
    public  FileUploadAndDownload fileUploadAndDownload;

    /**
     * 管理员添加汽车品牌
     * @param brandType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addBrandType",method = RequestMethod.POST)
    public ServerResponse<String> addBrandType(@RequestBody String brandType, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(brandType);
        Integer typeId=jsonObject.getInt("typeId");
        String brandName=jsonObject.getString("brandName");
        String companyName=jsonObject.getString("companyName");
        return adminService.addBrandType(typeId,brandName,companyName);

    }


    /**
     * 修改汽车品牌公司名称
     * @param brandType
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateBrandType",method = RequestMethod.POST)
    public ServerResponse<String> updateBrandType(@RequestBody BrandType brandType,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return adminService.updateBrandType(brandType);
    }


    /**
     * 管理员删除汽车品牌
     * @param brandType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteBrandType",method = RequestMethod.POST)
    public ServerResponse<String> deleteBrandType(@RequestBody String brandType,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(brandType);
        Integer brandTypeId=jsonObject.getInt("brandTypeId");
        return adminService.deleteBrandType(brandTypeId);
    }


    /**
     * 修改首页视频
     */
    @ResponseBody
    @RequestMapping(value = "/updateVideo",method = RequestMethod.POST)
        public ServerResponse updateVideo(MultipartFile file,Integer videoId,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        Video video=new Video();
       String videoFilePath=fileUploadAndDownload.upload(file);
       video.setVideoPath(videoFilePath);
        if(videoId==1){
            video.setVideoId(1);
        }else if(videoId==2){
            video.setVideoId(2);
        }
        return adminService.updateVideo(video);
    }

    /**
     * 获取到本地视频
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getIndexVideo",method = RequestMethod.POST)
    public ServerResponse getIndexVideo(HttpServletRequest request){
        return adminService.getIndexVideo();
    }


    /**
     * 获取到标题内容
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getTitle",method = RequestMethod.POST)
    public ServerResponse getTitle(HttpServletRequest request,@RequestBody String getTitle){
        JSONObject jsonObject=new JSONObject(getTitle);
        Integer titleId=jsonObject.getInt("titleId");
        return adminService.getTitle(titleId);
    }


    /**
     * 根据标题编号来修改内容
     * @param request
     * @param title
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateTitle",method = RequestMethod.POST)
    public ServerResponse updateTitle(HttpServletRequest request, @RequestBody Title title){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return adminService.updateTitle(title);
    }


    /**
     * 管理员根据源和数据编号来删除数据（批量删除）
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteSourceData",method = RequestMethod.POST)
    public ServerResponse deleteSourceData(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(data);
        Integer sourceId=jsonObject.getInt("sourceId");
        String dataIds=jsonObject.getString("dataIds");
        return adminService.deleteSourceData(sourceId,dataIds);
    }


    /**
     * 管理员对成绩添加评论
     * @param grade
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addDiscuss",method = RequestMethod.POST)
    public ServerResponse addDiscuss(@RequestBody Grade grade,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return adminService.addDiscuss(grade);
    }


    /**
     * 管理员添加帮助手册
     * @param manual
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addManual",method = RequestMethod.POST)
    public ServerResponse addManual(@RequestBody Manual manual,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return adminService.addManual(manual);
    }


    /**
     * 分页获取到帮助手册
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getManuals",method = RequestMethod.GET)
    public ServerResponse getManuals(Integer pageNum,Integer pageSize){
        return adminService.getManuals(pageNum,pageSize);
    }


    /**
     * 根据帮助手册编号来获取信息
     * @param manualId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSingleManual",method = RequestMethod.GET)
    public ServerResponse getSingleManual(Integer manualId,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return adminService.getSingleManual(manualId);
    }

    @ResponseBody
    @RequestMapping(value = "/updateManual",method = RequestMethod.POST)
    public ServerResponse updateManual(@RequestBody Manual manual,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        return adminService.updateManual(manual);
    }


    /**
     * 根据编号来删除手册信息
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteManual",method = RequestMethod.POST)
    public ServerResponse deleteManual(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        JSONObject jsonObject=new JSONObject(data);
        Integer manualId=jsonObject.getInt("manualId");
        return adminService.deleteManual(manualId);
    }




}
